#include "comm.hpp"
#include "Fifo.hpp"

int main()
{
	// 1、创建和获取共享空间唯一值
	key_t key = GetShmKeyOrDie();
	cout << "shm_key:" << ToHex(key) << endl;

	// 2、创建共享空间
	int shmid = CreatShm(key, defaultsize);
	cout << "shmid : " << shmid << endl;

	// 3、将共享内存和进程进行挂载（关联）
	char *addr = (char *)ShmAttach(shmid);
	cout << "server ShmAttach addr: " << ToHex((uint64_t)addr) << endl;
	// ShmDubge(shmid);
	//引入管道，实现进程协同
	Fifo fifo;
	Sysn syn;
	syn.OpenReadOrDie();

	// 通信
	while (1)
	{
		if (!syn.Wait())
			break;
		cout << "client say: " << addr << endl;
		sleep(1);
	}

	// 4、取消关联
	ShmDetach(addr);
	cout << "ShmDetach success,addr: " << ToHex((uint64_t)addr) << endl;

	// 释放共享空间
	DeleteShm(shmid);
	return 0;
}